/**
 * 
 */
package com.efast.cafe.util.jxl;

import java.awt.Color;

import jxl.format.Border;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Alignment;
import jxl.write.BorderLineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WriteException;

import org.springframework.stereotype.Component;

/**
 * @author Wangjianglin
 * FUNCTION : 类的主要功能描述
 *======================================================
 * CHANGE HISTORY
 *
 * YYMMDD    WHO       Description
 * -----------------------------------------------------
 * 2012-12-10   Name     	首次创建
 *======================================================
 */
@Component 
public class DefaultColumnFormat  implements IColumnFormat{  
  
    @Override  
    public WritableCellFormat getTitleCellFormat() {  
        WritableCellFormat wcf = null;     
           try {     
               //字体样式     
               WritableFont wf = new WritableFont(WritableFont.ARIAL,11, WritableFont.BOLD,false);//最后一个为是否italic     
               wf.setColour(Colour.BLACK);    
             
               wcf = new WritableCellFormat(wf);     
               //对齐方式     
               wcf.setAlignment(Alignment.CENTRE);     
               wcf.setVerticalAlignment(VerticalAlignment.CENTRE);     
               //边框     
               wcf.setBorder(Border.ALL,BorderLineStyle.THIN);     
                    
               //背景色     
               wcf.setBackground(getNearestColour("#C5D9F1"));     
           } catch (WriteException e) {     
            e.printStackTrace();     
           }     
           return wcf;      
    } 
    
    /**
     * 自定义颜色
     * @param strColor
     * @return
     */
    private Colour getNearestColour(String strColor) {  
        Color cl = Color.decode(strColor);  
        Colour color = null;  
        Colour[] colors = Colour.getAllColours();  
        if ((colors != null) && (colors.length > 0)) {  
           Colour crtColor = null;  
           int[] rgb = null;  
           int diff = 0;  
           int minDiff = 999;  
           for (int i = 0; i < colors.length; i++) {  
                crtColor = colors[i];  
                rgb = new int[3];  
                rgb[0] = crtColor.getDefaultRGB().getRed();  
                rgb[1] = crtColor.getDefaultRGB().getGreen();  
                rgb[2] = crtColor.getDefaultRGB().getBlue();  
      
                diff = Math.abs(rgb[0] - cl.getRed())  
                  + Math.abs(rgb[1] - cl.getGreen())  
                  + Math.abs(rgb[2] - cl.getBlue());  
                if (diff < minDiff) {  
                 minDiff = diff;  
                 color = crtColor;  
                }  
           }  
        }  
        if (color == null)  
           color = Colour.BLACK;  
        return color;  
    } 
  
}  

